﻿namespace Smart.Utils.System2
{
    using System;
    using System.Drawing;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;

    public class SystemMetricHelper
    {
        public static SystemMetricHelper NewFont = CurrentMetrics();
        public static SystemMetricHelper OldFont = CurrentMetrics();
        private NONCLIENTMETRICS pvParam;
        private const int SPI_GETNONCLIENTMETRICS = 0x29;
        private const int SPI_SETNONCLIENTMETRICS = 0x2a;

        public void Apply()
        {
            SystemParametersInfo(0x2a, this.pvParam.cbSize, this.pvParam, 1);
        }

        public static SystemMetricHelper CurrentMetrics()
        {
            NONCLIENTMETRICS pvParam = new NONCLIENTMETRICS();
            SystemParametersInfo(0x29, pvParam.cbSize, pvParam, 0);
            SystemMetricHelper helper = new SystemMetricHelper();
            helper.pvParam = pvParam;
            return helper;
        }

        public static void RestoreFont()
        {
            OldFont.Apply();
            MessageBox.Show("Font hệ thống đ\x00e3 được thay đổi.\n\nVui l\x00f2ng khởi động lại phần mềm để phần mềm được \x00e1p dụng cấu h\x00ecnh mới.", "Th\x00f4ng b\x00e1o");
        }

        public static void SetNewFont()
        {
            SystemMetricHelper helper = CurrentMetrics();
            helper.CaptionFont = new Font("Tahoma", 8f, FontStyle.Bold);
            helper.StatusFont = new Font("Tahoma", 8f, FontStyle.Regular);
            helper.Apply();
            MessageBox.Show("Font hệ thống đ\x00e3 được thay đổi.\n\nVui l\x00f2ng khởi động lại phần mềm để phần mềm được \x00e1p dụng.", "Th\x00f4ng b\x00e1o");
            Application.Restart();
        }

        [DllImport("user32.dll", CharSet=CharSet.Auto)]
        private static extern bool SystemParametersInfo(int Action, int uiParam, [In, Out] NONCLIENTMETRICS pvParam, int WinIni);

        public Font CaptionFont
        {
            get
            {
                return Font.FromLogFont(this.pvParam.lfCaptionFont);
            }
            set
            {
                value.ToLogFont(this.pvParam.lfCaptionFont);
            }
        }

        public Font MenuFont
        {
            get
            {
                return Font.FromLogFont(this.pvParam.lfMenuFont);
            }
            set
            {
                value.ToLogFont(this.pvParam.lfMenuFont);
            }
        }

        public Font MessageFont
        {
            get
            {
                return Font.FromLogFont(this.pvParam.lfMessageFont);
            }
            set
            {
                value.ToLogFont(this.pvParam.lfMessageFont);
            }
        }

        public Font SmCaptionFont
        {
            get
            {
                return Font.FromLogFont(this.pvParam.lfSmCaptionFont);
            }
            set
            {
                value.ToLogFont(this.pvParam.lfSmCaptionFont);
            }
        }

        public Font StatusFont
        {
            get
            {
                return Font.FromLogFont(this.pvParam.lfStatusFont);
            }
            set
            {
                value.ToLogFont(this.pvParam.lfStatusFont);
            }
        }

        [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
        private class LOGFONT
        {
            public int lfHeight;
            public int lfWidth;
            public int lfEscapement;
            public int lfOrientation;
            public int lfWeight;
            public byte lfItalic;
            public byte lfUnderline;
            public byte lfStrikeOut;
            public byte lfCharSet;
            public byte lfOutPrecision;
            public byte lfClipPrecision;
            public byte lfQuality;
            public byte lfPitchAndFamily;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst=0x20)]
            public string lfFaceName;
        }

        [StructLayout(LayoutKind.Sequential)]
        private class NONCLIENTMETRICS
        {
            public int cbSize = Marshal.SizeOf(typeof(SystemMetricHelper.NONCLIENTMETRICS));
            public int iBorderWidth;
            public int iScrollWidth;
            public int iScrollHeight;
            public int iCaptionWidth;
            public int iCaptionHeight;
            [MarshalAs(UnmanagedType.Struct)]
            public SystemMetricHelper.LOGFONT lfCaptionFont;
            public int iSmCaptionWidth;
            public int iSmCaptionHeight;
            [MarshalAs(UnmanagedType.Struct)]
            public SystemMetricHelper.LOGFONT lfSmCaptionFont;
            public int iMenuWidth;
            public int iMenuHeight;
            [MarshalAs(UnmanagedType.Struct)]
            public SystemMetricHelper.LOGFONT lfMenuFont;
            [MarshalAs(UnmanagedType.Struct)]
            public SystemMetricHelper.LOGFONT lfStatusFont;
            [MarshalAs(UnmanagedType.Struct)]
            public SystemMetricHelper.LOGFONT lfMessageFont;
        }
    }
}

